const JWT = require('../utils/JWT');
async function authMiddle(ctx, next) {
  let token = ctx.request.header.authorization;
  if (token) {
    token = token.replace(/Bearer /g, '');
    const result = JWT.verifyToken(token);
    if (result.code === -1) {
      ctx.status = 401;
      ctx.body = {
        code: 401,
        msg: '请重新登录'
      }
    } else {
      ctx.state.user = result;
    }
  }
  return next();
}
module.exports = () => authMiddle